//
//  WDADOrderManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/6.
//

#import "WDADOrderManager.h"
#import "WDBMSModel.h"
#import "WDBannerManager.h"
#import "WDRewardedVideoManager.h"
#import "WDInterstitialManager.h"
#import "WDSplashManager.h"
#import "WDHeader.h"
#import "WDNativeManager.h"
#import "WDNSLog.h"
#import "WDCardModel.h"
#import "WDCardZDYManager.h"
#import "WDCustomManager.h"
#import "WDCallBackModel.h"
#import <IronSource/IronSource.h>

@implementation WDADOrderManager
static WDADOrderManager *_order = nil;
+ (instancetype)shareOrderManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _order = [[self alloc] init];
        
    });
    return _order;
}

//hasOrder=NO时候调用的，去请求list的第一个类型的广告，有就展示，没有不展示
- (void)adLoadWithList:(NSArray *)list isCardAd:(BOOL)isCardAd
{
    if (list.count <= 0)
    {
        return;
    }
    for (int i = 0; i < list.count; i++) {
        NSString *order = [NSString stringWithFormat:@"%@",list[i]];
        if ([order intValue] == 1)
        {
            //1-激励视频
            [IronSource loadRewardedVideo];
            [WDRewardedVideoManager shareRewardedVideoManager].rewardedVideoDidLoadCallBackBlcok = ^(BOOL isSuccess) {
                if (isSuccess)
                {
                    [[WDRewardedVideoManager shareRewardedVideoManager] showIrSourceRV];
                }
            };
            
        }
        else if ([order intValue] == 2)
        {
            //2-插屏
            [IronSource loadInterstitial];
            [[WDInterstitialManager shareInterstitialManager] showInterstitial];
        }
        else if ([order intValue] == 3)
        {
            //3-banner
            [[WDBannerManager shareBannerManager] showBanner];
        }
        else if ([order intValue] == 4)
        {
            //4-splash
            [[WDSplashManager shareSplash] tryToPresentAd];
        }
        else if ([order intValue] == 5)
        {
            //5-native
            if (isCardAd)
            {
                [[WDNativeManager shareNative] loadNativeAd];
                [WDNativeManager shareNative].adLoaderCallBackBlcok = ^{
                    [[WDCardZDYManager shareCardZDYManager] showCardViewIsCustom:NO];
                };
            }else{
                [[WDNativeManager shareNative] showNativeAd:NO isJsonSource:NO];
            }
            
        }
        else if ([order intValue] == 6)
        {
            //6-自推广
            [[WDCustomManager shareCustomManager] showCustomAd];
        }
    }
}

//整体流程就是，无论调用哪个广告位，都要根据bms返回的顺序来判断展示哪种广告，先按照顺序判断广告是否有缓存，如有就展示，如无返回hasOrder=NO
- (BOOL)adOrderWithList:(NSArray *)list isCardAd:(BOOL)isCardAd
{
    if (list.count <= 0)
    {
        return NO;
    }
    WDLog(@"adOrder:%@",list);
    BOOL hasOrder = NO;
    NSMutableArray *temp = [NSMutableArray arrayWithArray:list];
//    [temp addObject:list.firstObject];
    for (int i = 0; i < temp.count; i++) {
        NSString *order = [NSString stringWithFormat:@"%@",temp[i]];
        if ([order intValue] == 1)
        {
            //1-激励视频
            if ([IronSource hasRewardedVideo])
            {
                hasOrder = YES;
                [[WDRewardedVideoManager shareRewardedVideoManager] showIrSourceRV];
                break;
            }
        }
        else if ([order intValue] == 2)
        {
            //2-插屏
            if ([IronSource hasInterstitial]){
                hasOrder = YES;
                [[WDInterstitialManager shareInterstitialManager] showInterstitial];
                break;
            }
        }
        else if ([order intValue] == 3)
        {
            //3-banner
            if ([WDBannerManager shareBannerManager].isLoadBanner){
                hasOrder = YES;
                [[WDHeader shareHeader] WDShowBanner];
                break;
            }
        }
        else if ([order intValue] == 4)
        {
            //4-splash
            if ([WDSplashManager shareSplash].isLoadSplash){
                hasOrder = YES;
                [[WDHeader shareHeader] WDShowSplashAdWithHasRemove:NO];
                break;
            }
        }
        else if ([order intValue] == 5)
        {
            //5-native
            if (isCardAd)
            {
                if ([WDNativeManager shareNative].isLoadNative){
                    hasOrder = YES;
                    [[WDCardZDYManager shareCardZDYManager] showCardViewIsCustom:NO];
                    break;
                }
            }else{
                if ([WDNativeManager shareNative].isLoadNative){
                    hasOrder = YES;
                    [[WDNativeManager shareNative] showNativeAd:NO isJsonSource:NO];
                    break;
                }
            }
            
            
        }
        else if ([order intValue] == 6)
        {
            //6-自推广
            
            if (isCardAd)
            {
                //卡片模式，先判断是否已经有获取到了原生模版了，有就用
                if ([WDCardModel shareCardModel].hasCardContent)
                {
                    hasOrder = YES;
                    [[WDCardZDYManager shareCardZDYManager] showCardViewIsCustom:YES];
                    break;
                }
                
            }else{
                //原生模版，先判断是否已经有获取到了原生模版了，有就用
                if ([WDCardModel shareCardModel].hasCardContent)
                {
                    hasOrder = YES;
                    [[WDHeader shareHeader] WDShowCustom];
                    break;
                }
            }
        }
    }
    return hasOrder;
}

@end
